package ch.selise.exercise.pattern.notepad.command;

import ch.selise.exercise.pattern.notepad.command.impl.AddCommand;
import ch.selise.exercise.pattern.notepad.command.impl.DeleteCommand;
import ch.selise.exercise.pattern.notepad.command.impl.EditCommand;
import ch.selise.exercise.pattern.notepad.command.impl.ExitCommand;
import ch.selise.exercise.pattern.notepad.command.impl.PrintCommand;
import ch.selise.exercise.pattern.notepad.command.impl.RedoCommand;
import ch.selise.exercise.pattern.notepad.command.impl.UndoCommand;
import ch.selise.exercise.pattern.notepad.exception.UnsupportedCommandException;

public class CommandCreationFactory {
	public static ICommand getCommand(GetCommandParameter parameterObject, StackManipulator stackManipulator)
			throws UnsupportedCommandException {

		ICommand command = null;
		if (parameterObject.commandText.equalsIgnoreCase("exit")) {
			if (parameterObject.isScannerSupported()) {
				command = new ExitCommand(parameterObject.in);
			} else {
				throw new UnsupportedCommandException(
						"No need of EXIT command.", new Exception());
			}
		}

		if (parameterObject.commandText.equalsIgnoreCase("undo")) {
			command = new UndoCommand(stackManipulator);
		}

		if (parameterObject.commandText.equalsIgnoreCase("redo")) {
			command = new RedoCommand(stackManipulator);
		}

		if (parameterObject.commandText.equalsIgnoreCase("print")) {
			command = new PrintCommand();
		}

		if (parameterObject.commandText.equalsIgnoreCase("add")) {
			if (parameterObject.isScannerSupported()) {
				command = new AddCommand(parameterObject.in);
			} else {
				command = new AddCommand(parameterObject.text);
			}

		}

		if (parameterObject.commandText.equalsIgnoreCase("edit")) {
			if (parameterObject.isScannerSupported()) {
				command = new EditCommand(parameterObject.in);
			} else {
				command = new EditCommand(parameterObject.text,
						parameterObject.index);
			}
		}

		if (parameterObject.commandText.equalsIgnoreCase("delete")) {
			if (parameterObject.isScannerSupported()) {
				command = new DeleteCommand(parameterObject.in);
			} else {
				command = new DeleteCommand(parameterObject.index);
			}
		}

		if (command == null) {
			throw new UnsupportedCommandException(parameterObject.commandText
					+ " is not a valid command", new Exception());
		}
		return command;
	}

}
